<!DOCTYPE html>
<html>
  
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  <meta name="author" content="Amos Zhu">
  
  
  <title>SpringBoot整合RabbitMQ——消息的发送和接收 | Amos的技术博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="rabbitMQ,Spring Boot,rabbitMQ,">
  

  
  <meta name="description" content="上篇博文我们整理了RabbitMQ的交换机、队列以及路由绑定等相关知识，并且了解了RabbitMQ是如何发送消息给队列的，以及重要的RoutingKey等重要知识点，这篇博文我们来重点了解下RabbitMQ是如何发送消息，消费消息的，本片博文我们还是以代码为主，简要说明为辅，同时对消息的发送和接收功能进行简单的封装，可以作为一个jar包给第三方进行使用。">

  

  
    <script src="//cdn.jsdelivr.net/npm/leancloud-storage@3.11.1/dist/av-min.js" async></script>
  

  
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
  

  
    <script src="//unpkg.com/valine/dist/Valine.min.js" async></script>
  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"eObxCkTJ6j4GzRxGLgiHyfJa-gzGzoHsz","appkey":"fzRMAFC6MCE11mp7mnvh4Kwe","comment":true,"count":true},
    welcome: {"enable":true,"interval":30},
    start_time: "2019-05-28",
    passwords: ["efe07af7441da2b69c4a41e42e73be4db47f66010a56900788a458354a7373ec", ],
    is_post: true,
    lock: false,
    author: "Amos Zhu",
    share: {"qq":true,"wechat":true},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  <script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>

  
    <link rel="icon" href="/blog/images/favicon.ico">
    <link rel="apple-touch-icon" href="/blog/images/touch-icon.png">
  

  <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">

  
</head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">Amos Zhu</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 当浮躁的心静不下，不妨写些东西或者看会书</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">首页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/redis/" target="_self">redis</a>
      
        <a href="/blog/rabbitMQ/" target="_self">rabbitMQ</a>
      
        <a href="/blog/elasticsearch" target="_self">elasticsearch</a>
      
        <a href="/blog/stuff" target="_self">stuff</a>
      
        <a href="/blog/kafka" target="_self">kafka</a>
      
        <a href="/blog/friends/" target="_self">友链</a>
      
        <a href="/blog/about/" target="_self">关于</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://gitee.com/amos_zhu" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            首页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/redis/" target="_self">
            redis
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/rabbitMQ/" target="_self">
            rabbitMQ
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/elasticsearch" target="_self">
            elasticsearch
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/stuff" target="_self">
            stuff
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/kafka" target="_self">
            kafka
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/friends/" target="_self">
            友链
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/about/" target="_self">
            关于
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2019-07-03
    </span>
    
      <span>
        | <a href="/blog/categories/rabbitMQ/"><i class="fa fa-bookmark"></i>rabbitMQ</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
  </div>
  <h1 class="passage-title">
    SpringBoot整合RabbitMQ——消息的发送和接收
  </h1>
  
  <article class="passage-article">
    <br>本博文是在上一篇博文<a href="http://amos_zhu.gitee.io/blog/passages/SpringBoot整合RabbitMQ——交换机和队列的管理和绑定/">《SpringBoot整合RabbitMQ——交换机和队列的管理和绑定》</a>的基础上进行重构和新增消息的发送和接收的功能<p></p>
<p>如果我们要提供一个类似jar包的可以让第三方来作为依赖引入，从而在代码中简单集成我们提供的rabbitMQ的通用的功能，我们的项目需要满足以下的需求：</p>
<ul>
<li>消息发送方需要知道消息是否真正的到达了RabbitMQ,如果发送不到rabbitMQ，如何保证发送方能够处理发送失败的业务数据</li>
<li>如何保证消息可靠的达到消费者，并且消费者能否成功消费消息，并且告知发送者消费者的具体消费情况（改业务需求主要是满足消息发送方需要知道消息消费方是否消费成功），对于是否通知消息发送方具体的消费情况需要做到用户自定义</li>
<li>为了确保消息的正确和安全，对于消息要做到持久化，防止出现异常导致消息丢失</li>
<li>保证客户端对于消息能自主的进行消息的确认，并且对了消费失败的数据能在业务方保存日志、</li>
<li>客户端引入我们的依赖，可以很方便的在程序中动态实现队列的注册和交换机的绑定，并且很方便的实现消息的发送以及发送失败的处理</li>
<li>客户端能很方便的继承消息的消费，动态的指定处理器自定义处理消息，并且提供消息消费失败（包括业务流程的失败和系统代码错误引起的失败）后消息回传的功能，回传功能做到业务可控</li>
<li>在客户端消费消息存在日志记录，并且能与业务逻辑解耦，实现业务与日志分离</li>
<li>能针对客户端发出的各类不同类型的消息进行处理，并且是实现各种不同业务的处理扩展</li>
</ul>
<h2 id="消息的发送"><a href="#消息的发送" class="headerlink" title="消息的发送"></a>消息的发送</h2><p>在进行消息的发送之前，我们需要了解下参数<code>mandatory</code>：</p>
<ul>
<li>当其值为true时，<strong>交换器无法根据自身的类型和路由键匹配到符合条件的队列</strong>，这是rabbitMQ就会通过回调函数将消息返回给生产者。</li>
<li>当其值为false时，如果出现上述情形，则消息会丢失</li>
</ul>
<p>如果需要处理发送rabbitMQ失败的话，在SpringBoot中我们需要在配置文件中配置如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  rabbitmq:</span><br><span class="line">    template:</span><br><span class="line">      mandatory: <span class="literal">true</span></span><br><span class="line">    publisher-confirms: <span class="literal">true</span></span><br><span class="line">    publisher-returns: <span class="literal">true</span></span><br></pre></td></tr></table></figure>
<p>对应的代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// RabbitMQConfig类中添加属性</span></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 消息发送失败，是否回调给发送者</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Value</span>(<span class="string">"$&#123;spring.rabbitmq.template.mandatory:false&#125;"</span>)</span><br><span class="line"><span class="keyword">private</span> Boolean mandatory;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 是否确认</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Value</span>(<span class="string">"$&#123;spring.rabbitmq.publisher-confirms:false&#125;"</span>)</span><br><span class="line"><span class="keyword">private</span> Boolean publisherConfirms;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 如果mandatorys设置成true，该值也设置 成true</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Value</span>(<span class="string">"$&#123;spring.rabbitmq.publisher-returns:false&#125;"</span>)</span><br><span class="line"><span class="keyword">private</span> Boolean publisherReturns;</span><br><span class="line"></span><br><span class="line"><span class="comment">// RabbitMQConfig中定义connectionFactory中设置属性</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ConnectionFactory <span class="title">connectionFactory</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    CachingConnectionFactory cachingConnectionFactory = <span class="keyword">new</span> CachingConnectionFactory();</span><br><span class="line">    cachingConnectionFactory.setAddresses(<span class="keyword">this</span>.addresses);</span><br><span class="line">    cachingConnectionFactory.setUsername(<span class="keyword">this</span>.username);</span><br><span class="line">    cachingConnectionFactory.setPassword(<span class="keyword">this</span>.password);</span><br><span class="line">    cachingConnectionFactory.setVirtualHost(<span class="keyword">this</span>.virtualHost);</span><br><span class="line">    <span class="comment">// 如果消息要设置成回调，则以下的配置必须要设置成true</span></span><br><span class="line">    cachingConnectionFactory.setPublisherConfirms(<span class="keyword">this</span>.publisherConfirms);</span><br><span class="line">    cachingConnectionFactory.setPublisherReturns(<span class="keyword">this</span>.publisherReturns);</span><br><span class="line">    <span class="keyword">return</span> cachingConnectionFactory;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 同时为了调用SpringBoot集成rabbitMQ提供的发送的方法，我们需要注入rabbitTemplate</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 因为要设置回调类，所以应是prototype类型，如果是singleton类型，则回调类为最后一次设置</span></span><br><span class="line"><span class="comment"> * 主要是为了设置回调类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span>(name = <span class="string">"rabbitTemplate"</span>)</span><br><span class="line"><span class="meta">@Scope</span>(ConfigurableBeanFactory.SCOPE_PROTOTYPE)</span><br><span class="line"><span class="function"><span class="keyword">public</span> RabbitTemplate <span class="title">rabbitTemplate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    RabbitTemplate template = <span class="keyword">new</span> RabbitTemplate(<span class="keyword">this</span>.connectionFactory());</span><br><span class="line">    template.setMessageConverter(<span class="keyword">new</span> Jackson2JsonMessageConverter());</span><br><span class="line">    <span class="keyword">return</span> template;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上我们就完成了配置类的修改，下面我们来对发送消息的方法进行封装，并且支持用户自定义相关属性<br>在发送消息之前，我们需要先创建队列，并且将交换机（这里采用默认的交换机mq.direct）和队列进行绑定，路由键就设置成队列名，方法中提供自定义的绑定方法，如有需要可以自行进行封装使用<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 发送消息的抽象类，子类可以实现该类来处理对应的业务逻辑</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * 抽象类实现了ConfirmCallback和ReturnCallback接口，</span></span><br><span class="line"><span class="comment"> * confirmCallback来实现业务日志记录，并且自定义处理各自的业务处理逻辑</span></span><br><span class="line"><span class="comment"> * returnCallback来实现消息发送失败时的业务处理，并且自定义各自的业务处理逻辑</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractSendService</span> <span class="keyword">implements</span> <span class="title">RabbitTemplate</span>.<span class="title">ConfirmCallback</span>, <span class="title">RabbitTemplate</span>.<span class="title">ReturnCallback</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(<span class="keyword">this</span>.getClass());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">static</span> String DEFAULT_EXCHANGE = <span class="string">"amq.direct"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    RabbitTemplate rabbitTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 简单的发送消息</span></span><br><span class="line"><span class="comment">     * 发送的交换机是默认的 amq.direct交换机，该交换机的类型是DIRECT类型，开启持久化机制</span></span><br><span class="line"><span class="comment">     * 发送的队列即为RoutingKey,需要绑定队列时</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> queue   队列，默认是跟路由键是相同的</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content 发送的内容</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String queue, String content)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(queue)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"发送的队列不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(content)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"内容不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.send(MqExchange.DEFAULT_DIRECT_EXCHANGE, queue, content, <span class="keyword">null</span>, UUIDUtils.generateUuid());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 发送一条有过期时间的消息</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> queue      队列，默认是跟路由键相同的</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content    发送的内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> expireTime 过期时间 时间毫秒</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String queue, String content, <span class="keyword">int</span> expireTime)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(queue)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"发送的队列不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(content)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"内容不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        MessagePostProcessor messagePostProcessor = <span class="keyword">new</span> MessagePostProcessor() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Message <span class="title">postProcessMessage</span><span class="params">(Message message)</span> <span class="keyword">throws</span> AmqpException </span>&#123;</span><br><span class="line">                <span class="comment">// 设置消息的过期时间</span></span><br><span class="line">                message.getMessageProperties().setExpiration(expireTime + <span class="string">""</span>);</span><br><span class="line">                <span class="keyword">return</span> message;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="keyword">this</span>.send(MqExchange.DEFAULT_DIRECT_EXCHANGE, queue, content, messagePostProcessor, UUIDUtils.generateUuid());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 按照给定的交换机、路由键、发送内容、发送的自定义属性来发送消息</span></span><br><span class="line"><span class="comment">     * TODO 待完善交互方式</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> exchange             交换机名称</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> routingKey           路由键</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> object               发送的内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messagePostProcessor 发送消息自定义处理</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageId            消息ID</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String exchange, String routingKey, Object object, MessagePostProcessor messagePostProcessor, String messageId)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(exchange)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"交换机不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(routingKey)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"路由键不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(object)) &#123;</span><br><span class="line">            RabbitMQExceptionUtils.throwRabbitMQException(<span class="string">"发送的内容不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        CorrelationData correlationData = <span class="keyword">new</span> CorrelationData();</span><br><span class="line">        correlationData.setId(StringUtils.isEmpty(messageId) ? UUIDUtils.generateUuid() : messageId);</span><br><span class="line">        MqMessage mqMessage = <span class="keyword">new</span> MqMessage();</span><br><span class="line">        mqMessage.setMessageBody(object);</span><br><span class="line">        mqMessage.setMessageId(correlationData.getId());</span><br><span class="line">        mqMessage.setExchangeName(exchange);</span><br><span class="line">        mqMessage.setQueueName(routingKey);</span><br><span class="line">        mqMessage.setRoutingKey(routingKey);</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(messagePostProcessor)) &#123;</span><br><span class="line">            <span class="keyword">this</span>.rabbitTemplate.convertAndSend(exchange, routingKey, mqMessage, correlationData);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 发送对应的消息</span></span><br><span class="line">            <span class="keyword">this</span>.rabbitTemplate.convertAndSend(exchange, routingKey, mqMessage, messagePostProcessor, correlationData);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 默认实现发送确认的处理方法</span></span><br><span class="line"><span class="comment">     * 子类需要重写该方法，实现自己的业务处理逻辑</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageId 消息</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ack</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> cause</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">handleConfirmCallback</span><span class="params">(String messageId, <span class="keyword">boolean</span> ack, String cause)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 默认实现发送匹配不上队列时 回调函数的处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> replyCode</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> replyText</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> routingKey</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">handleReturnCallback</span><span class="params">(Message message, <span class="keyword">int</span> replyCode, String replyText,</span></span></span><br><span class="line"><span class="function"><span class="params">                                              String routingKey)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 交换机如果根据自身的类型和路由键匹配上对应的队列时，是否调用returnCallback回调函数</span></span><br><span class="line"><span class="comment">     * true: 调用returnCallback回调函数</span></span><br><span class="line"><span class="comment">     * false： 不调用returnCallback回调函数 这样在匹配不上对应的队列时，会导致消息丢失</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;spring.message.mandatory:false&#125;  "</span>)</span><br><span class="line">    <span class="keyword">private</span> Boolean mandatory;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 默认队列的优先级</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MESSAGE_PRIORITY = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostConstruct</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"sendservice 初始化...... "</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.rabbitTemplate.setConfirmCallback(<span class="keyword">this</span>);</span><br><span class="line">        <span class="keyword">this</span>.rabbitTemplate.setReturnCallback(<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 确认后回调方法</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> correlationData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ack</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> cause</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">confirm</span><span class="params">(CorrelationData correlationData, <span class="keyword">boolean</span> ack, String cause)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"confirm-----correlationData:"</span> + correlationData.toString() + <span class="string">"---ack:"</span> + ack + <span class="string">"----cause:"</span> + cause);</span><br><span class="line">        <span class="comment">// TODO 记录日志（数据库或者es）</span></span><br><span class="line">        <span class="keyword">this</span>.handleConfirmCallback(correlationData.getId(), ack, cause);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 失败后回调方法</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> replyCode</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> replyText</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> exchange</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> routingKey</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">returnedMessage</span><span class="params">(Message message, <span class="keyword">int</span> replyCode, String replyText, String exchange, String routingKey)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"return-----message:"</span> + message.toString() + <span class="string">"---replyCode:"</span> + replyCode + <span class="string">"----replyText:"</span> + replyText + <span class="string">"----exchange:"</span> + exchange + <span class="string">"----routingKey:"</span> + routingKey);</span><br><span class="line">        <span class="comment">// TODO 记录日志（数据库或者es）</span></span><br><span class="line">        <span class="keyword">this</span>.handleReturnCallback(message, replyCode, replyText, routingKey);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="消息的接收"><a href="#消息的接收" class="headerlink" title="消息的接收"></a>消息的接收</h2><p>消息发送RabbitMQ之后，我们需要定义监听来监控队列， 并且消费队列上的消息，本类方法中对消息消费进行了封装，添加了消费信息日志和状态的记录，并且支持用户自定义消费方法。消费完成之后，可以自定义设置是否返回给消息发送者消息消费的具体情况，并且针对不同类型的消息 ，封装了命令模式来处理不同类型的消息，方便用户后期对消息的处理的扩展，具体的代码可以参考Gitee上项目<br><a href="https://gitee.com/amos_zhu/rabbitmq" target="_blank" rel="noopener">rabbitmq</a></p>
<p>主要代码逻辑如下:</p>
<ol>
<li>注册队列，并且使用队列名来与交换机进行绑定</li>
<li>为该队列添加好消息接收处理</li>
<li>封装消息接收的处理逻辑，并且对于消息接收失败的进行重发</li>
</ol>
<p>下面是重要代码说明：</p>
<p>注册队列，并且为该队列设置消息监听<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span>: rabbitmq</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: RegisterQueue</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span>: com.amos.common.register</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: zhuqb</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 注册队列并且设置监听</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: 2019/7/2 0002 下午 15:32</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span>: V1.0</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractRegisterQueue</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(<span class="keyword">this</span>.getClass());</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    AmBindDeclare amBindDeclare;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    AmQueueDeclare amQueueDeclare;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    MessageListen messageListen;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;spring.rabbitmq.queue.isAck:false&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> Boolean isAck;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 子类提供自定义的消息监听</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> AbstractMessageHandler <span class="title">messageHandler</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 实例化队列名</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> queue</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AbstractRegisterQueue <span class="title">queue</span><span class="params">(String queue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.queue = queue;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 实例化交换机</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> exchange</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AbstractRegisterQueue <span class="title">exchange</span><span class="params">(String exchange)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.exchange = exchange;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 实例化路由键</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> routingKey</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AbstractRegisterQueue <span class="title">routingKey</span><span class="params">(String routingKey)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.routingKey = routingKey;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 实例化结构化属性</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> properties</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AbstractRegisterQueue <span class="title">properties</span><span class="params">(Map&lt;String, Object&gt; properties)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.properties = properties;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 队列名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String queue;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 交换机 默认是 amq.direct 交换机</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String exchange = MqExchange.DEFAULT_DIRECT_EXCHANGE;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 路由键 默认是队列名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String routingKey = <span class="keyword">this</span>.getQueue();</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 结构化属性</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Map&lt;String, Object&gt; properties;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getRoutingKey</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(<span class="keyword">this</span>.routingKey)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.getQueue();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.routingKey;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注册队列，并且监听队列</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">registerQueue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        MqQueue mqQueue = <span class="keyword">new</span> MqQueue().name(<span class="keyword">this</span>.queue);</span><br><span class="line">        <span class="keyword">this</span>.amQueueDeclare.declareQueue(mqQueue);</span><br><span class="line">        <span class="keyword">boolean</span> tag = <span class="keyword">this</span>.amBindDeclare.bind(<span class="keyword">this</span>.queue, Binding.DestinationType.QUEUE, <span class="keyword">this</span>.exchange, <span class="keyword">this</span>.getRoutingKey(), <span class="keyword">this</span>.properties);</span><br><span class="line">        <span class="keyword">if</span> (tag) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.messageListen.addMessageLister(<span class="keyword">this</span>.queue, <span class="keyword">this</span>.messageHandler(), <span class="keyword">this</span>.isAck);</span><br><span class="line">                <span class="keyword">return</span> Boolean.TRUE;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> Boolean.FALSE;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> tag;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>上面类主要是用来注册队列，并且注册成功之后为其新增消息监听类</p>
<p>我们再来新增消息监听以及消息接收处理的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.listen</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> AbstractMessageHandle</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 队列设置监听基类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 20:21</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MessageListen</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(<span class="keyword">this</span>.getClass());</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ConnectionFactory connectionFactory;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在容器中加入消息监听</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> queue</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageHandler</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> isAck</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addMessageLister</span><span class="params">(String queue, AbstractMessageHandler messageHandler, <span class="keyword">boolean</span> isAck)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        SimpleMessageListenerContainer container = <span class="keyword">new</span> SimpleMessageListenerContainer();</span><br><span class="line">        container.setConnectionFactory(<span class="keyword">this</span>.connectionFactory);</span><br><span class="line">        container.setQueueNames(queue);</span><br><span class="line">        AcknowledgeMode ack = AcknowledgeMode.NONE;</span><br><span class="line">        <span class="keyword">if</span> (isAck) &#123;</span><br><span class="line">            ack = AcknowledgeMode.MANUAL;</span><br><span class="line">        &#125;</span><br><span class="line">        messageHandler.setAck(queue, ack);</span><br><span class="line">        container.setAcknowledgeMode(ack);</span><br><span class="line">        MessageListenerAdapter adapter = <span class="keyword">new</span> MessageListenerAdapter(messageHandler);</span><br><span class="line">        container.setMessageListener(adapter);</span><br><span class="line">        container.start();</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"------ 已成功监听异步消息触发通知队列："</span> + queue + <span class="string">" ------"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>指明队列的监听类，并且维护是否手动ack消息<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.listen</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> AbstractMessageHandler</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 消息接收处理类</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * 实现 ChannelAwareMessageListener接口 重写onMessage方法来实现业务的处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 20:09</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractMessageHandler</span> <span class="keyword">implements</span> <span class="title">ChannelAwareMessageListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(<span class="keyword">this</span>.getClass());</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;spring.message.queue.retryTimes:5&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> Integer retryTimes;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 用户自定义消息处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message 消息</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">handleMessage</span><span class="params">(String message, Channel channel)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ConcurrentHashMap&lt;String, AcknowledgeMode&gt; ackMap = <span class="keyword">new</span> ConcurrentHashMap&lt;&gt;(<span class="number">8</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 消息处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message 消息体</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> channel channel通道</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onMessage</span><span class="params">(Message message, Channel channel)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"接收到发送的消息......."</span>);</span><br><span class="line">        <span class="comment">// 业务处理是否成功</span></span><br><span class="line">        <span class="keyword">boolean</span> handleResult = <span class="keyword">false</span>;</span><br><span class="line">        <span class="comment">// 消息处理标识</span></span><br><span class="line">        <span class="keyword">long</span> deliveryTag = message.getMessageProperties().getDeliveryTag();</span><br><span class="line">        <span class="comment">// 获取消费的队列名</span></span><br><span class="line">        String queue = message.getMessageProperties().getConsumerQueue();</span><br><span class="line"></span><br><span class="line">        MqMessage mqMessage = <span class="keyword">null</span>;</span><br><span class="line">        <span class="comment">// TODO 进行自己的业务处理 比如记录日志</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            String msg = <span class="keyword">new</span> String(message.getBody());</span><br><span class="line">            mqMessage = JSONObject.parseObject(msg, MqMessage.class);</span><br><span class="line">            <span class="comment">// 自定义业务处理</span></span><br><span class="line">            <span class="keyword">this</span>.handleMessage(JSONObject.toJSONString(mqMessage.getMessageBody()), channel);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// TODO 如果消息处理失败，处理失败的采取措施， 确保消息不丢失</span></span><br><span class="line">        <span class="keyword">this</span>.onMessageCompleted(mqMessage, queue, channel, deliveryTag, handleResult);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 消息处理结束后进行复处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> mqMessage    消息实体</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> queue</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> channel</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> deliveryTag</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> handleResult 业务处理是否成功</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">onMessageCompleted</span><span class="params">(MqMessage mqMessage, String queue, Channel channel, <span class="keyword">long</span> deliveryTag, <span class="keyword">boolean</span> handleResult)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"消息："</span> + mqMessage.toString() + <span class="string">"处理完成，等待事务提交和状态更新"</span>);</span><br><span class="line">        <span class="keyword">if</span> (!handleResult) &#123;</span><br><span class="line">            <span class="comment">// TODO 业务处理失败，需要更新状态</span></span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        AcknowledgeMode ack = <span class="keyword">this</span>.ackMap.get(queue);</span><br><span class="line">        <span class="keyword">if</span> (ack.isManual()) &#123;</span><br><span class="line">            <span class="comment">//重试5次</span></span><br><span class="line">            <span class="keyword">int</span> retryTimes = <span class="number">5</span>;</span><br><span class="line">            <span class="comment">//进行消息</span></span><br><span class="line">            RetryTemplate oRetryTemplate = <span class="keyword">new</span> RetryTemplate();</span><br><span class="line">            SimpleRetryPolicy oRetryPolicy = <span class="keyword">new</span> SimpleRetryPolicy();</span><br><span class="line">            oRetryPolicy.setMaxAttempts(retryTimes);</span><br><span class="line">            oRetryTemplate.setRetryPolicy(oRetryPolicy);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="comment">// obj为doWithRetry的返回结果，可以为任意类型</span></span><br><span class="line">                Integer result = oRetryTemplate.execute(<span class="keyword">new</span> RetryCallback&lt;Integer, Exception&gt;() &#123;</span><br><span class="line">                    <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> Integer <span class="title">doWithRetry</span><span class="params">(RetryContext context)</span> <span class="keyword">throws</span> Exception </span>&#123;<span class="comment">//开始重试</span></span><br><span class="line">                        channel.basicAck(deliveryTag, <span class="keyword">false</span>);</span><br><span class="line">                        AbstractMessageHandler.<span class="keyword">this</span>.logger.info(<span class="string">"消息"</span> + mqMessage.toString() + <span class="string">"已签收"</span>);</span><br><span class="line">                        <span class="keyword">return</span> ++<span class="keyword">this</span>.count;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;, <span class="keyword">new</span> RecoveryCallback&lt;Integer&gt;() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> Integer <span class="title">recover</span><span class="params">(RetryContext context)</span> <span class="keyword">throws</span> Exception </span>&#123; <span class="comment">//重试多次后都失败了</span></span><br><span class="line">                        AbstractMessageHandler.<span class="keyword">this</span>.logger.info(<span class="string">"消息"</span> + mqMessage.toString() + <span class="string">"签收失败"</span>);</span><br><span class="line">                        <span class="keyword">return</span> Integer.MAX_VALUE;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (result.intValue() &lt;= retryTimes) &#123;</span><br><span class="line">                    <span class="comment">//消息签收成功 更改状态</span></span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">//MQ服务器或网络出现问题，签收失败 更改状态</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                <span class="keyword">this</span>.logger.error(<span class="string">"消息"</span> + mqMessage.toString() + <span class="string">"签收出现异常："</span> + e.getMessage());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.info(<span class="string">"消息自动签收"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ack</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Title</span>: setAck</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@date</span>: 2018年9月14日 上午11:17:41</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span>: 注入消息签收模式</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">setAck</span><span class="params">(String queue, AcknowledgeMode ack)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.ackMap.put(queue, ack);</span><br><span class="line">        <span class="keyword">this</span>.logger.info(<span class="string">"注入队列 "</span> + queue + <span class="string">" 消息签收模式: "</span> + ack.name());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>上面代码主要是封装了消息接收处理的代码逻辑</p>
<ol>
<li>定义抽象类方便让子类来继承实现基类中的方法，其次类实现了<code>ChannelAwareMessageListener</code>接口，实现<code>onMessage</code>方法的重写</li>
<li>该方法中除了进行自身业务的处理，同时也调用业务的自定义消息处理逻辑</li>
<li>对于消息接收处理失败后，进行消息重发，并且可以业务进行日志记录</li>
<li>在对队列进行设置监听时，指定是否手动ack消息</li>
<li>业务新增类继承<code>AbstractMessageHandler</code>基类，并且将该监听类与队列动态绑定即可</li>
</ol>
<p>以上就是本博文对消息的发送和接收处理进行的简单的封装，其中核心的业务都已经实现，待后期与elasticsearch集合完善日志记录相关的功能</p>
<p>附： <strong>不同类型消息的处理</strong></p>
<p>之所以不跟上面的代码整合在一起，主要是因为本rabbitMQ的项目主要是为了对rabbitMQ的常用业务进行封装，消息的处理大多数是业务方面的工作，如果整合在一起的会造成代码的耦合，不利于rabbitMQ功能代码的剥离。</p>
<p>不同消息类型的处理的业务流程如下：</p>
<ol>
<li><p>声明一个接口定义消息处理的通用方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 对消息进行处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Receiver</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 对消息进行处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">HandleResult <span class="title">handleMessage</span><span class="params">(MessageData messageData)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>定义消息接收基类，该类实现<code>Receiver</code>接口,积累中定义不同类型处理的自定义方法以及处理成功和处理失败的业务逻辑</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 定义通用消息接收处理基类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractReceiver</span> <span class="keyword">implements</span> <span class="title">Receiver</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(AbstractReceiver.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 用户自定义消息处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> HandleResult <span class="title">exec</span><span class="params">(MessageData messageData)</span> <span class="keyword">throws</span> Exception</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 用户自定义验证</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> Result <span class="title">validate</span><span class="params">(MessageData messageData)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 成功处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> HandleResult <span class="title">handleSuccess</span><span class="params">(MessageData messageData)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 失败处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> HandleResult <span class="title">handleFail</span><span class="params">(MessageData messageData)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> HandleResult <span class="title">handleMessage</span><span class="params">(MessageData messageData)</span> </span>&#123;</span><br><span class="line">        logger.info(<span class="keyword">this</span>.getClass().getSimpleName() + <span class="string">"--&gt;handleMessage()参数 unicomData:&#123;&#125;"</span>, messageData.toString());</span><br><span class="line">        HandleResult handleResult = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 如果自定义验证不通过</span></span><br><span class="line">            Result result = <span class="keyword">this</span>.validate(messageData);</span><br><span class="line">            <span class="keyword">if</span> (!ResultEnum.success().equals(result.getCode())) &#123;</span><br><span class="line">                <span class="comment">// 如果验证失败 进行失败处理</span></span><br><span class="line"></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.handleFail(messageData);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 根据自行处理的返回结果</span></span><br><span class="line">            handleResult = <span class="keyword">this</span>.exec(messageData);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 执行成功处理的逻辑</span></span><br><span class="line">            handleResult = <span class="keyword">this</span>.handleSuccess(messageData);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            messageData.setContent(e.getMessage());</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.handleFail(messageData);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> handleResult;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>针对不同的消息类型实体进行处理 ，这里可以采用命令模式来封装代码，首先定义命令的基类，基类中定义好处理的方法，子类实现该基类，并且设置对应的命令来处理其业务逻辑，该种设计模式将命令与处理者之间进行了松耦合，可以很方便的维护命令与处理者之间关系</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//命令的基类</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 命令抽象类</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * 把接收消息的类型封装成一个命令 并且交给指定的接收者出处理</span></span><br><span class="line"><span class="comment"> * 方便扩展每个命令的处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractCommand</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 每个命令都必须被处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> messageData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> HandleResult <span class="title">execute</span><span class="params">(MessageData messageData)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 回调命令的处理，在回调命令中设置了抽象处理者，处理者交由子类去具现</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 回调函数 消息处理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CallBackCommand</span> <span class="keyword">extends</span> <span class="title">AbstractCommand</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 定义handler来进行命令处理</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> AbstractHandler handler;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CallBackCommand</span><span class="params">(AbstractHandler handler)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.handler = handler;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> CallBackCommand <span class="title">init</span><span class="params">(AbstractHandler handler)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.handler = handler;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 执行业务处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> unicomData</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HandleResult <span class="title">execute</span><span class="params">(MessageData unicomData)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.handler.handle(unicomData);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>定义具体的处理者来处理不同的消息<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义处理者基类</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 处理消息 抽象类</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * 业务处理需要继承该基类，实现处理的方法</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractHandler</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 自定义处理</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> HandleResult <span class="title">handle</span><span class="params">(MessageData data)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 子类实现处理者基类并且实现具体的处理方法</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright © 2018 五月工作室. All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Package</span> com.amos.common.send</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span> SendService</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 回调消息处理者</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> Amos</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Modifier</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2019/7/1 15:11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span> 1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CallBackHandler</span> <span class="keyword">extends</span> <span class="title">AbstractHandler</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 修改消息</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HandleResult <span class="title">handle</span><span class="params">(MessageData data)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// TODO  自定义业务逻辑处理</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> HandleResult.CallBack(<span class="keyword">true</span>).callback(<span class="keyword">false</span>).msg(<span class="string">"处理成功"</span>).builder();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#消息的发送"><span class="toc-text">消息的发送</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#消息的接收"><span class="toc-text">消息的接收</span></a></li></ol>
  </div>
</aside>
  
    <aside class="passage-copyright">
      <div>本文作者: Amos Zhu</div>
      
        <div>
          原文链接: 
          <a href target="_blank">http://amos_zhu.gitee.io/passages/SpringBoot整合RabbitMQ——消息的发送和接收/</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/blog/tags/Spring-Boot/"><i class="fa fa-tags"></i>Spring Boot</a>
     
      <a href="/blog/tags/rabbitMQ/"><i class="fa fa-tags"></i>rabbitMQ</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    <div class="site-footer-wrapper">
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">博客推荐</h5>
          
            <span class="site-footer-item">
              <a href="http://youzhixueyuan.com/series/java" target="_blank">优知学院</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://ruanyifeng.com/" target="_blank">阮一峰的个人网站</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">系列教程</h5>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/redis" target="_blank">Redis系列教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/rabbitMQ/" target="_blank">RabbitMQ教程</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://amos_zhu.gitee.io/blog/kafka/" target="_blank">Kafka架构探险</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">抓到我</h5>
          
            <span class="site-footer-item">
              <a href="https://gitee.com/amos_zhu" target="_blank">Gitee</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://blog.csdn.net/wdcl2468" target="_blank">CSDN</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://www.jianshu.com/u/2dbe61b1f3c3" target="_blank">简书</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: amoszhu@aliyun.com
      </div>
    
    <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div>
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <div class="site-layer-input-choose">
          <a href="javascript:void(0);" title="Change Search Engine">Google</a>
        </div>
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">
  <div class="bottom-bar-left">
    <a href="/blog/passages/SpringBoot整合RabbitMQ——RabbitMQ进阶/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/passages/SpringBoot整合RabbitMQ——交换机和队列的管理和绑定/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
      <a href="javascript:void(0);" id="site-reward">
        <i class="fa fa-thumbs-up"></i>
      </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
    <a id="share-btn-wechat" href="javascript:void(0);" target="_blank">
      <i class="fa fa-wechat"></i>
    </a>
  
</div>
    


  <script async>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>



  <script async>
    (function(){
    var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?":"https://jspassport.ssl.qhimg.com/11.0.1.js?";
    document.write('<script src="' + src + '" id="sozz"><\/script>');
    })();
  </script>


    
  </body>
</html>